import Cookies from "js-cookie";
import { defineStore } from "pinia";

const useAppStore = defineStore("app", {
	/**初始化数据 **/
	state: () => ({
		// 侧边栏状态
		sidebar: {
			// 侧边栏是否打开
			opened: Cookies.get("sidebarStatus") ? Cookies.get("sidebarStatus") : true,
			// 侧边栏是否动画
			withoutAnimation: false,
			// 侧边栏是否隐藏
			hide: false,
		},
		// 设备类型
		device: "desktop",
		// 全局尺寸
		size: Cookies.get("size") || "default",
		// 缓存页面
		cachedViews: [] as string[],
	}),
	actions: {
		//切换侧边栏
		toggleSideBar(withoutAnimation: any) {
			if (this.sidebar.hide) {
				return false;
			}
			this.sidebar.opened = !this.sidebar.opened;
			this.sidebar.withoutAnimation = withoutAnimation;
			if (this.sidebar.opened) {
				Cookies.set("sidebarStatus", "1");
			} else {
				Cookies.set("sidebarStatus", "0");
			}
		},
		// 关闭侧边栏
		closeSideBar(withoutAnimation: any) {
			Cookies.set("sidebarStatus", "0");
			this.sidebar.opened = false;
			this.sidebar.withoutAnimation = withoutAnimation;
		},
		// 切换设备类型
		toggleDevice(device: string) {
			this.device = device;
		},
		// 设置全局尺寸
		setSize(size: any) {
			this.size = size;
			Cookies.set("size", size);
		},
		// 切换侧边栏隐藏
		toggleSideBarHide(status: any) {
			this.sidebar.hide = status;
		},
		// 清除所有缓存页面
		delAllCachedViews() {
			this.cachedViews = [];
		},
	},
});

export default useAppStore;
